﻿using System;
using System.Collections.Generic;
using System.Web;
using WAD.Model;
using WAD.DAL;
using System.Text;
using System.Collections.Specialized;

namespace WAD.WEB.AppCode
{
    public class AdminAjax : QD.Web.Ajax.AjaxPage
    {
        protected int adminId { set; get; }

        /// <summary>
        /// 加載時執行
        /// </summary>
        protected override void PageLoad()
        {
            if (!Request.HttpMethod.Equals("POST"))
            {
                //限制请求方式为POST
                //JsonResult(Message.Code.NO_PERMISSION, Message.Msg.NO_PERMISSION);
            }
            adminId = Parse.Int("adminid", 0);
            IsLogin();
        }
        /// <summary>
        /// 判断是否登陆
        /// </summary>
        protected void IsLogin()
        {
            if (Request.QueryString.Count == 0)
                return;

            List<string> listUrl = new List<string>();
            listUrl.Add("/admin/ajax/user.ashx?login");
            listUrl.Add("/admin/ajax/user.ashx?getlist_agent");             //代理商下的终端客户
            listUrl.Add("/admin/ajax/user.ashx?getdetail");
            listUrl.Add("/admin/ajax/agent.ashx?get");
            listUrl.Add("/admin/ajax/agent.ashx?add");                      //添加代理商
            listUrl.Add("/admin/ajax/agent.ashx?getlist_recoad");           //代理商的资金流水
            listUrl.Add("/admin/ajax/agent.ashx?pay");                      //代理商管理后台充值
            listUrl.Add("/admin/ajax/agent.ashx?getlist_tidan");            //渠道部统计微街秀提单
            listUrl.Add("/admin/ajax/fc_cash.ashx?check_ali");


            string url = string.Format("{0}?{1}", Request.Url.AbsolutePath, Request.QueryString[0].ToLower()).ToLower();
            if (!listUrl.Contains(url))
            {
                if (adminId < 1)
                    JsonResult(21, "缺少管理员参数");
                HttpCookie cookie = Context.Request.Cookies["adminid"];
                if (cookie == null)
                    JsonResult(21, "请重新登录");
                if (adminId != Convert.ToInt32(cookie.Value))
                    JsonResult(21, "管理员ID无效");
            }
        }

        /// <summary>
        /// 操作成功
        /// </summary>
        protected void Success()
        {
            JsonResult(Message.Code.SUCCESS, Message.Msg.SUCCESS);
        }

        /// <summary>
        /// 操作成功
        /// </summary>
        protected void Success(object item)
        {
            JsonResult(Message.Code.SUCCESS, Message.Msg.SUCCESS, item);
        }

        /// <summary>
        /// 操作失败
        /// </summary>
        protected void Fail()
        {
            JsonResult(Message.Code.FAIL, Message.Msg.FAIL);
        }

        /// <summary>
        /// 沒有数据
        /// </summary>
        protected void DataEmpty()
        {
            JsonResult(Message.Code.DATA_EMPTY, Message.Msg.DATA_EMPTY);
        }

        /// <summary>
        /// 参数错误
        /// </summary>
        protected void ParamError()
        {
            JsonResult(Message.Code.PARAM_ERROR, Message.Msg.PARAM_ERROR);
        }

        /// <summary>
        /// ajax中未捕获的异常处理
        /// </summary>
        /// <param name="e"></param>
        protected override void ErrorCatch(Exception e)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Type:").Append(e.GetType().Name).Append("\n");
            sb.Append("Message:").Append(e.Message).Append("\n");
            sb.Append("Class:").Append(this.GetType()).Append("\n");
            sb.Append("URL:").Append(Request.Url).Append("\n");

            //QueryString参数
            NameValueCollection QueryString = Request.QueryString;
            sb.Append("QueryString参数:");
            foreach (string key in QueryString.Keys)
            {
                sb.Append(key).Append(" = ").Append(QueryString[key]).Append("\t"); ;
            }
            sb.Append("\n");

            //Form参数
            NameValueCollection Form = Request.Form;
            sb.Append("Form参数:");
            foreach (string key in Form.Keys)
            {
                sb.Append(key).Append(" = ").Append(Form[key]).Append("\t"); ;
            }
            sb.Append("\n");

            //Cookie参数
            sb.Append("Cookie参数:");
            for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
            {
                sb.Append(Request.Cookies.Keys[i]).Append(" = ").Append(Request.Cookies[i].Value.ToString()).Append("\t");

            }
            sb.Append("\n");

            //文件列表
            sb.Append("文件列表:");
            HttpFileCollection files = Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                sb.Append("name = ").Append(files[i].FileName).Append("\t");
            }

            sb.Append("\n");


            SavaLog("Error", sb.ToString());

            JsonResult(Message.Code.SYSTEM_ERROR, Message.Msg.SYSTEM_ERROR);
        }


        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="type"></param>
        /// <param name="content"></param>
        protected void SavaLog(String type, String content)
        {
            MLogcat m = new MLogcat() { content = content, type = type };
            DaoLogcat.Add(m);
        }

        protected bool CheckCaiWu()
        {
            if (Request.Cookies["adminname"] == null || Request.Cookies["adminname"].Value != "zuohong")
                return false;
            return true;
        }
    }
}